package com.xjm.ips.controller.statistics;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.xjm.ips.entity.Department;
import com.xjm.ips.entity.MyPage;
import com.xjm.ips.service.RedisService;
import com.xjm.ips.service.ViewService;
import org.apache.poi.ss.formula.functions.T;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/statistics/personnel")
public class ViewController {
    @Autowired
    ViewService viewService;
    @Autowired
    RedisService redisService;

    @GetMapping("/getEmpLateandTakeLeave")
    public MyPage getEmpLateandTakeLeave(@RequestParam(defaultValue = "1") Integer pageNo,
                                         @RequestParam(defaultValue = "10") Integer pageSize){
        return viewService.getEmpLateandTakeLeave(pageNo,pageSize);
    }

    @GetMapping("/getDepartmentNumbers")
    public MyPage getDepartmentNumbers(@RequestParam(defaultValue = "1") Integer pageNo,
                                       @RequestParam(defaultValue = "10") Integer pageSize){
        List<Department> departmentList = null;
        if(redisService.exists("departmentNumbers")){
            departmentList =  ((List<Department>) redisService.get("departmentNumbers"));
        }else {
            departmentList = viewService.getDeparmentNumbers();
            redisService.set("departmentNumbers",departmentList);
        }
        List<Department> departments = pageBySubList(departmentList, pageSize, pageNo);
        MyPage myPage = new MyPage();
        myPage.setData(departments);
        myPage.setTotal(18L);
        return myPage;
    }


    @GetMapping("/getDepartmentRewardOrPenalty")
    public MyPage getDepartmentRewardOrPenalty(@RequestParam(defaultValue = "1") Integer pageNo,
                                       @RequestParam(defaultValue = "10") Integer pageSize){
        List<Department> departmentList = null;
        if(redisService.exists("departmentRewardOrPenalty")){
            departmentList =  ((List<Department>) redisService.get("departmentRewardOrPenalty"));
        }else {
            departmentList = viewService.getDepartmentRewardOrPenalty();
            redisService.set("departmentRewardOrPenalty",departmentList);
        }
        List<Department> departments = pageBySubList(departmentList, pageSize, pageNo);
        MyPage myPage = new MyPage();
        myPage.setData(departments);
        myPage.setTotal(18L);
        return myPage;
    }


    /**
     * 利用subList方法进行分页
     * @param list 分页数据
     * @param pagesize  页面大小
     * @param currentPage   当前页面
     */
    public static List<Department> pageBySubList(List<Department> list, int pagesize, int currentPage) {
        int totalcount = list.size();
        int pagecount = 0;
        List<Department> subList;
        int m = totalcount % pagesize;
        if (m > 0) {
            pagecount = totalcount / pagesize + 1;
        } else {
            pagecount = totalcount / pagesize;
        }
        if (m == 0) {
            subList = list.subList((currentPage - 1) * pagesize, pagesize * (currentPage));
        } else {
            if (currentPage == pagecount) {
                subList = list.subList((currentPage - 1) * pagesize, totalcount);
            } else {
                subList = list.subList((currentPage - 1) * pagesize, pagesize * (currentPage));
            }
        }
        return subList;
    }

}
